<script>
	import {
		checkUser
	} from '@/common/api/common.js'
	export default {
		onLaunch: async () => {
			console.log('App Launch')

			let telegramId = ''
			if (!uni.getStorageSync('telegramId') && !uni.$tg.getTelegramId()) {
				return;
			}
			if (uni.getStorageSync('telegramId') && uni.$tg.getTelegramId()) {
				if (uni.$tg.getTelegramId() != uni.getStorageSync('telegramId')) {
					telegramId = uni.$tg.getTelegramId()
				} else {
					telegramId = uni.getStorageSync('telegramId')
				}
			} else if (uni.getStorageSync('telegramId') && !uni.$tg.getTelegramId()) {
				telegramId = uni.getStorageSync('telegramId')
			} else if (!uni.getStorageSync('telegramId') && uni.$tg.getTelegramId()) {
				telegramId = uni.$tg.getTelegramId()
			}
			const res = await checkUser({
				telegramId: telegramId
			})
			if (res.code == 200 && res.data?.user && res.data?.user?.email) {
				uni.setStorageSync('token', res.data.token)
				uni.setStorageSync('userInfo', res.data.user)
				
				uni.switchTab({
					url: '/pages/index/index'
				})
			} else {
				uni.navigateTo({
					url: '/pages/register/index'
				})
			}

		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-plus/index.scss";

	page {
		color: #1F1F1F;
	}

	// 公共背景
	.pagebg {
		width: 100vw;
		min-height: 100vh;
		background: linear-gradient(to right bottom, #1890FF10, #1890FF01);
		font-size: 28rpx;
	}
</style>